<!DOCTYPE html>
<html lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<!-- Meta, title, CSS, favicons, etc. -->
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">

		<title>权限设置</title>

		<!-- Bootstrap -->
		<link href="css/bootstrap.min.css" rel="stylesheet">
		<!-- Font Awesome -->
		<link href="css/font-awesome.min.css" rel="stylesheet">
		<!-- NProgress -->
		<!-- Custom Theme Style -->
		<link href="css/custom.min.css" rel="stylesheet">
		
		
	</head>

	<body class="nav-md">
		<div class="container body">
			<div class="main_container">
				<div id="head">
						</div>
<div class="right_col" role="main" style="min-height: 3366px;">
				<div class="col-md-12 col-sm-12 col-xs-12">
              <div class="x_panel">
                <div class="x_title">
                  <h2>权限分配 </h2>
                  
                  <div class="clearfix"></div>
                </div>
                <div class="" id="role">




                </div>

              </div>
            </div>
	<div class="form-group">
		<div class="col-md-12 col-md-offset-4">
			<a  href="javascript:;" class="btn btn-success" id="updateRole">修改</a>
			<a href="javascript:;" class="btn btn-primary" onclick="window.history.back(-1)">返回</a>

		</div>
	</div>
</div>

		</div>
        </div>
		<!-- jQuery -->
		<script src="js/jquery.min.js"></script>
		<!-- Bootstrap -->
		<script src="js/bootstrap.min.js"></script>
		<!-- FastClick -->
		<script src="js/fastclick.js"></script>
		<!-- NProgress -->
		<script src="js/nprogress.js"></script>
		<!-- validator -->
		<script src="js/validator.js"></script>
		<script src="js/echarts.js"></script>
		<!-- Custom Theme Scripts -->

		<!-- validator -->
		<script>
				
				
				$(document).ready(function(){
					$("#head").load("head.html");
					$(".checkbox1").click(function  () {
					var $check=	$(this).prop("checked");
					if($check==true){
						$(".checkbox2").prop("checked", true);
					}else if($check==false){
						$(".checkbox2").prop("checked", false);
					}
					});
					$(".checkbox11").click(function  () {
					var $check=	$(this).prop("checked");
					if($check==true){
						$(".checkbox12").prop("checked", true);
					}else if($check==false){
						$(".checkbox12").prop("checked", false);
					}
					});
					$(".checkbox111").click(function  () {
					var $check=	$(this).prop("checked");
					if($check==true){
						$(".checkbox112").prop("checked", true);
					}else if($check==false){
						$(".checkbox112").prop("checked", false);
					}
					});
				});
			// initialize the validator function
			validator.message.date = 'not a real date';

			// validate a field on "blur" event, a 'select' on 'change' event & a '.reuired' classed multifield on 'keyup':
			$('form')
				.on('blur', 'input[required], input.optional, select.required', validator.checkField)
				.on('change', 'select.required', validator.checkField)
				.on('keypress', 'input[required][pattern]', validator.keypress);

			$('.multi.required').on('keyup blur', 'input', function() {
				validator.checkField.apply($(this).siblings().last()[0]);
			});

			$('form').submit(function(e) {
				e.preventDefault();
				var submit = true;

				// evaluate the form using generic validaing
				if (!validator.checkAll($(this))) {
					submit = false;
				}

				if (submit)
					this.submit();

				return false;
			});
                var $url=window.location.href;
                var $typeId=$url.split("=")[1];
                var $token = window.sessionStorage.getItem('token');
                $.ajax({
                    type: "post",
                    async:false,
                    xhrFields: {withCredentials: true},
                    url: "http://localhost:8081/role/toRole",
                    dataType: "json",
                    data: {"token": $token},
                    success: function (data) {
                        $.each(data, function (i, depot) {
                            if(depot.id<2000){
                              var $parentId=depot.id;
                            $("#role").append('<div class="" style="float: left;">\n' +
                                '                    <ul class=" " style="list-style: none;">\n' +
                                '                      <li>\n' +
                                '                          <div class="icheckbox_flat-green" style="position: relative;display: inline-block;">\n' +
                                '                          \t<input type="checkbox" name="check1" class="aa" value="'+depot.id+'" /> </div> <span style="font-size: 15px;color: black;font-weight: bolder;">'+depot.fName+'</span>\n' +
                                '                     <ul class="x_content" style="list-style: none;" id="'+depot.id+'">\n' +

                                '                     </ul>\n' +
                                '                      </li>\n' +
                                '                    </ul>\n' +
                                '                  </div>');
                                $(data).each(function () {
                                    if($parentId==this.parentid){
                                        $("#"+$parentId).append(' <li>\n' +
                                            '                        <p>\n' +
                                            '                          <div class="icheckbox_flat-green" style="position: relative;display: inline-block;">\n' +
                                            '                          \t<input type="checkbox" name="check1" class="checkbox2" value="'+this.id+'" /> </div> '+this.fName+' </p>\n' +
                                            '                      </li> ');
                                    }
                                });
                            }


                        })

                        $.ajax({
                            type: "post",
                            xhrFields: {withCredentials: true},
                            url: "http://localhost:8081/role/typefun",
                            dataType: "json",
                            data: {"token": $token,"typeId":$typeId},
                            success: function (data) {
                                // var checkID = [];//定义一个空数组

                                // alert($("input[name='check1']").length);
                                $.each(data, function (i, depot) {
                                    $("input[name='check1']").each(function () {
                                        if (depot.functionalid == $(this).val()) {
                                            $(this).prop({checked: true});
                                        }
                                    });
                                })
                                // })
                            },
                            error: function () {
                                alert("错误");
                            }
                        })
                    },
                    error: function () {
                        alert("错误");
                    }
                })

                    $(document).on("click", ".aa", function () {
                    var $check=$(this).prop("checked");
                    if($check==true){
                        $(this).parent().parent().children("ul").children().children("div").children("input").prop("checked", true);
                    }else if($check==false){
                        $(this).parent().parent().children("ul").children().children("div").children("input").prop("checked", false);
                    }
                });
                $(document).on("click", "#updateRole", function () {
                    var checkID = [];//定义一个空数组
                    $("input[name='check1']:checked").each(function(i){//把所有被选中的复选框的值存入数组
                                   checkID[i] =$(this).val();
                            });
                        $.ajax({
                            async : false,
                            cache : false,
                            traditional: true,
                            type: "post",
                            xhrFields: {withCredentials: true},
                            url: "http://localhost:8081/role/updateRole",
                            dataType: "json",
                            data: {"token": $token,"typeId":$typeId,"checkIDs":checkID},
                            success: function (data) {
                                if(data.message=="yes"){
									window.location.href="role.html";
								}else{
                                    alert("修改权限失败")
								}
                            },
                            error: function () {
                                alert("错误");
                            }
                        })

                });




		</script>
		<!-- /validator -->
	</body>
</html>
